<template>
  <div>
      <!-- 搜索表单 -->
    <el-form ref="searcForm" :inline="true" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="name" style="width: 200px">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item prop="linkman" style="width: 200px" v-if="!isDialog">
        <el-input v-model="searchMap.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="mobile" style="width: 200px" v-if="!isDialog">
        <el-input v-model="searchMap.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="feachData">查询</el-button>
      </el-form-item>
      <el-form-item v-if="!isDialog">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
      <el-form-item v-if="!isDialog">
        <el-button @click="resetForm('searcForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 供应商数据列表 -->
    <el-table :data="list" height="380" border style="width: 100%" highlight-current-row @current-change="clickCurrentChange">
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="供应商名称"></el-table-column>
      <el-table-column prop="linkman" label="联系人" width="100"></el-table-column>
      <el-table-column prop="mobile" label="联系电话" v-if="!isDialog"></el-table-column>
      <el-table-column prop="remark" label="备注" v-if="!isDialog"></el-table-column>
      <el-table-column label="操作" width="150" v-if="!isDialog"> 
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :layout="!isDialog ? 'total, sizes, prev, pager, next, jumper' : 'prev, pager, next'"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="10"
      :total="total"
    ></el-pagination>
    <!-- 对话框 -->
    <el-dialog title="供应商编辑" :visible.sync="dialogFormVisible" width="500px">
        <el-form :model="pojo" ref="pojoForm" label-width="100px" label-posion="right" style="width: 400px" :rules="rules">
            <el-form-item label="供应商名称" prop="name">
                <el-input v-model="pojo.name"></el-input>
            </el-form-item>
            <el-form-item label="联系人" prop="linkman">
                <el-input v-model="pojo.linkman"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
                <el-input v-model="pojo.mobile"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="pojo.remark"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="pojo.id == null ? addData('pojoForm') : update('pojoForm')">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import supplierApi from "@/api/supplier";

export default {
  props:{
    isDialog: Boolean
  },

  data() {
    return {
      list: [],
      total: 0,
      currentPage: 0,
      pageSize: 10,
      searchMap:{
          name: '',
          linkman: '',
          mobile: ''
      },
      dialogFormVisible: false,
      pojo:{
          id: null,
          name: '',
          linkman: '',
          mobile: '',
          remark: ''
      },
      rules: {
          name: [
              {required: true, message: '供应商名称不能为空', trigger: 'blur'}
          ],
          linkman: [
              {required: true, message: '联系人不能为空', trigger: 'blur'}
          ]
      }
    };
  },

  created() {
    this.feachData();
  },

  methods: {
    // 获取供应商数据
    feachData() {
      // console.log('123')
      supplierApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
        const resp = response.data
        this.list = resp.data.rows
        this.total = resp.data.total
      });
    },
    // 编辑供应商信息
    handleEdit(id){
        // console.log('编辑', id)
        this.handleAdd()
        supplierApi.getById(id).then(response => {
            const resp = response.data
            if(resp.flag){
                this.pojo = resp.data
            }
        })
    },
    // 删除供应商
    handleDelete(id){
      this.$confirm('确认删除这条记录吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(() => {
        console.log('确认')
         supplierApi.deleteById(id).then(response => {
          const resp = response.data
          this.$message({
            message: resp.message,
            type: resp.flag ? 'success' : 'error'
          })
          if(resp.flag){
              this.feachData()
          }
        })
      }).catch(() => {
        console.log('取消')
      })
    },
    // 分页
    handleSizeChange(val){
        this.pageSize = val
        this.feachData()
    },
    handleCurrentChange(val){
        this.currentPage = val
        this.feachData()
    },
    // 添加供应商
    handleAdd(){
        this.dialogFormVisible = true
        this.$nextTick(() => {
            this.$refs['pojoForm'].resetFields()
      })
    },
    // 重置搜索框
    resetForm(fromName){
        // console.log('重置搜索框')
        this.$refs[fromName].resetFields()
    },
    // 确认添加供应商
    addData(fromName){
        this.$refs[fromName].validate(valid => {
            if(valid){
                supplierApi.add(this.pojo).then(response => {
                    const resp = response.data
                    if(resp.flag){
                        console.log('添加成功')
                        this.dialogFormVisible = false
                    }else{
                        this.$message({
                            message: resp.message,
                            type: 'warning'
                        })
                    }
                })
            }else{
                return false
            }
        })
        // console.log('abc')
    },
    // 更新供应商信息
    update(fromName){
        this.$refs[fromName].validate(valid => {
            if(valid){
                supplierApi.update(this.pojo).then(response => {
                    const resp = response.data
                    if(resp.flag){
                        this.feachData()
                        this.dialogFormVisible = false
                    }else{
                        this.$message({
                            message: resp.message,
                            type: 'warning'
                        })
                    }
                })
            }else{
                return false
            }
        })
    },
    // 触发父子组件传递
    clickCurrentChange(currentRow){
      this.$emit('option-supplier', currentRow)
    }
  }
};
</script>

<style scoped>
.demo-form-inline {
  margin-top: 20px;
}
</style>